package com.biyesheji.utils;

import org.gavaghan.geodesy.Ellipsoid;
import org.gavaghan.geodesy.GeodeticCalculator;
import org.gavaghan.geodesy.GeodeticCurve;
import org.gavaghan.geodesy.GlobalCoordinates;

import java.util.HashMap;

import java.util.Map;

public class MapDistance1 {

    private static double EARTH_RADIUS = 6378.137;

    private static double rad(double lat1) {

        return lat1 * Math.PI / 180.0;

    }
    /**

     * 根据两个位置的经纬度，来计算两地的距离(单位为KM)

     * 参数为String类型

     * @param // lat1 用户经度

     * @param // lng1 用户纬度

     * @param // lat2 商家经度

     * @param // lng2 商家纬度

     * @return

     */

    public static String getDistance(String lat1Str, String lng1Str, String lat2Str, String lng2Str) {

        double lat1 = Double.parseDouble(lat1Str);

        double lng1 = Double.parseDouble(lng1Str);

        double lat2 = Double.parseDouble(lat2Str);

        double lng2 = Double.parseDouble(lng2Str);

        double patm = 2;

        double radLat1 = rad(lat1);

        double radLat2 = rad(lat2);

        double difference = radLat1 - radLat2;

        double mdifference = rad(lng1) - rad(lng2);

        double distance = patm * Math.asin(Math.sqrt(Math.pow(Math.sin(difference / patm), patm)

                + Math.cos(radLat1) * Math.cos(radLat2)

                * Math.pow(Math.sin(mdifference / patm), patm)));

        distance = distance * EARTH_RADIUS;

        String distanceStr = String.valueOf(distance);

        return distanceStr;

    }

    /**

     * 获取当前用户一定距离以内的经纬度值

     * 单位米 return minLat

     * 最小经度 minLng

     * 最小纬度 maxLat

     * 最大经度 maxLng

     * 最大纬度 minLat

     */

    public static Map getAround(String latStr, String lngStr, String raidus) {

        Map map = new HashMap();

        double latitude = Double.parseDouble(latStr);// 传值给经度

        double longitude = Double.parseDouble(lngStr);// 传值给纬度

        double degree = (24901 * 1609) / 360.0; // 获取每度

        double raidusMile = Double.parseDouble(raidus);

        double mpdLng = Double.parseDouble((degree * Math.cos(latitude * (Math.PI / 180))+"").replace("-", ""));

        double dpmLng = 1 / mpdLng;

        double radiusLng = dpmLng * raidusMile;

//获取最小经度

        double minLat = longitude - radiusLng;

// 获取最大经度

        double maxLat = longitude + radiusLng;

        double dpmLat = 1 / degree;

        double radiusLat = dpmLat * raidusMile;

// 获取最小纬度

        double minLng = latitude - radiusLat;

// 获取最大纬度

        double maxLng = latitude + radiusLat;



        map.put("minLng", minLng+"");

        map.put("maxLng", maxLng+"");

        map.put("minLat", minLat+"");

        map.put("maxLat", maxLat+"");

        return map;

    }

/*

    public static  double getDistanceMeter(GlobalCoordinates gpsFrom, GlobalCoordinates gpsTo, Ellipsoid ellipsoid){
        //创建GeodeticCalculator,调用计算方法,传入坐标系,经纬度用于计算距离
        GeodeticCurve geoCurve = new GeodeticCalculator().calculateGeodeticCurve(ellipsoid, gpsFrom, gpsTo);
        return geoCurve.getEllipsoidalDistance();
    }
*/

    public static void main(String[] args) {

        System.out.println(getDistance("121.4954633187","31.260706743526","121.49864314503","31.257659060141"));
        System.out.println(getDistance("111.154481","21.782305 ","111.117372","21.729256"));

        System.out.println(getAround("117.11811", "36.68484", "13000"));

        System.out.println("-----------------------------------");

/*

        //给定两个坐标系,计算两点相差距离
        GlobalCoordinates source = new GlobalCoordinates(111.154481, 21.782305);
        GlobalCoordinates target = new GlobalCoordinates(111.116641,21.728736);
        //Sphere坐标的计算结果
        double meter1 =getDistanceMeter(source,target,Ellipsoid.Sphere);
        //WGS84坐标系计算结果
        double meter2 = getDistanceMeter(source,target,Ellipsoid.WGS84);

        //计算结果Sphere 坐标系的计算结果与 WGS84坐标系的计算结果存在几十米的误差，不同的坐标系精度不同,
        System.out.println("Sphere 坐标系的计算结果是" + meter1 + "M");
        System.out.println("wGS84  坐标系的计算结果是" + meter2 + "M");
*/

    }


}
